#include "BSUtility.h"
#include "base/Utility.h"
#include "HorizentalBox.h"
#include "VerticalBox.h"
#include "ListView.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));
void HorizentalBox::ParseXmlElement(class TiXmlElement* ele)
{
	if (strcmp(ele->Value(), "HorizentalBox") == 0)
	{
		TiXmlNode* ti_control_node = ele->FirstChild();
		TiXmlElement* ele = ti_control_node->ToElement();
		auto& vattr = Utility::GetTiXmlElementAttributes(ele);
		for (int i = 0; i < vattr.size(); i++) {
			SetAttr(vattr[i].data());
		}
		while (ele)
		{
			Control* ctrl = CreateControl(ele->Value());
			ctrl->DoParseXmlElement(ele);

			TiXmlAttribute* ta = ele->FirstAttribute();
			while (ta)
			{
				ctrl->SetAttribute(ta->Name(), ta->Value());
				/*	if (strcmp(ta->Name(), "pos") == 0)
						{
							int x, y;
							sscanf_s(ta->Value(), "%d,%d", &x, &y);
							ctrl->pos.x = x;
							ctrl->pos.y = y;
						}
						else if (strcmp(ta->Name(), "size") == 0)
						{
							int w, h;
							sscanf_s(ta->Value(), "%d,%d", &w, &h);
							ctrl->size.x = w;
							ctrl->size.y = h;
						}
						else if (strcmp(ta->Name(), "name") == 0)
						{
							sCtrlName = ta->Value();
						}*/
						/*else if (strcmp(ta->Name(), "bkcolor") == 0 || strcmp(ta->Name(), "bkclr") == 0)
							{
								sCtrlName = ta->Value();
								int w, h;
								ctrl->size.x = w;
								ctrl->size.y = h;
							}
							else if (strcmp(ta->Name(), "hovercolor") == 0 || strcmp(ta->Name(), "hoverclr") == 0)
							{
								UITheme::
							}*/

				ta = ta->Next();
			}

			TString sCtrlName = ctrl->name.val.data();
			if (ctrl)
			{
				childs.push_back(std::make_pair(sCtrlName, ctrl));
			}
			ele = ele->NextSiblingElement();
		}
	}
}
bool HorizentalBox::SetAttribute(const char* attrName, const char* attrVal)
{
	if (attrVal == nullptr) return false;
	if (Control::SetAttribute(attrName, attrVal)) {
		return true;
	}
	if (strcmp(attrName, "bkcolor") == 0)
	{
		sClrBk = attrVal;
		/*int x, y,z;
		sscanf_s(attrVal, "%d,%d,%d", &x, &y,&z);
		this->clrBk.r = x;
		this->clrBk.g = y;
		this->clrBk.b = z;*/
		return true;
	}
	else  if (strcmp(attrName, "size") == 0)
	{
		int x, y;
		sscanf_s(attrVal, "%d,%d", &x, &y);
		size.x = x;
		size.y = y;
		return true;
	}
	else  if (strcmp(attrName, "pos") == 0)
	{
		int x, y;
		sscanf_s(attrVal, "%d,%d", &x, &y);
		pos.x = x;
		pos.y = y;
		return true;
	}
	return true;
}
void HorizentalBox::UpdateLayout()
{
	if (vcChildpos.size() == 0)
	{
		for (int i = 0; i < childs.size(); i++)
		{
			vcChildpos.push_back(childs[i].second->pos);
		}
	}
	//  bNeedUpdateLayout = false;
	int curX = 0;
	for (int i = 0; i < childs.size(); i++)
	{
		childs[i].second->pos.x.val = vcChildpos[i].x.val + pos.x.val + curX;
		childs[i].second->pos.y.val = vcChildpos[i].y.val + pos.y.val;
		curX += childs[i].second->size.x.val;
	}
}
void HorizentalBox::OnDraw()
{
	FocusControl(false);
	UpdateLayout();
	//  DrawBorder();
	glBegin(GL_LINE_LOOP);
	glVertex3f(pos.x.val, pos.y.val, 0);
	glVertex3f(pos.x.val + size.x.val, pos.y.val, 0);
	glVertex3f(pos.x.val + size.x.val, pos.y.val + size.y.val, 0);
	glVertex3f(pos.x.val, pos.y.val + size.y.val, 0);
	glEnd();
	for (int i = 0; i < childs.size(); i++)
	{
		Control* cld = childs[i].second;
		cld->Draw();
	}
}
void HorizentalBox::onMouseMove(int x, int y)
{

}
void HorizentalBox::onMouseButtonDown(int button, int x, int y)
{
	for (int i = 0; i < childs.size(); i++)
	{
		Control* cld = childs[i].second;
		Vec2vbi point;
		point.x.val = x;
		point.y.val = y;
		if (BSUtility::HitTest(point, cld->pos, cld->size).val)
		{
			cld->onMouseButtonDown(button, x, y);
		}
	}
}
Control* HorizentalBox::CreateControl(std::string sControlClassName)
{
	Control* ctrl = dynamic_cast<Control*>(BlueprintScriptLibrary::BlueprintDataPool::GetInstance()->createInstFuncs[sControlClassName]());
	ctrl->rootWindow = rootWindow;
	ctrl->parent = std::shared_ptr<Control>(this);
	return ctrl;
}
Control* HorizentalBox::GetControlById(std::string ctrlName)
{
	for (int i = 0; i < childs.size(); i++)
	{
		if (childs[i].second->name.val == ctrlName)
		{
			return childs[i].second;
		}
		HorizentalBox* hb = dynamic_cast<HorizentalBox*>(childs[i].second);
		if (hb != nullptr)
		{
			Control* ctrl = hb->GetControlById(ctrlName);
			if (ctrl != nullptr)
			{
				return ctrl;
			}
		}
		VerticalBox* vb = dynamic_cast<VerticalBox*>(childs[i].second);
		if (vb != nullptr)
		{
			Control* ctrl = vb->GetControlById(ctrlName);
			if (ctrl != nullptr)
			{
				return ctrl;
			}
		}
	}
	return nullptr;
}
void HorizentalBox::onMouseButtonUp(int button, int x, int y)
{
	for (int i = 0; i < childs.size(); i++)
	{
		Control* cld = childs[i].second;
		Vec2vbi point;
		point.x.val = x;
		point.y.val = y;
		if (BSUtility::HitTest(point, cld->pos, cld->size).val)
		{
			cld->onMouseButtonUp(button, x, y);
		}
	}
}
void HorizentalBox::onKeyUp(int key)
{
	for (int i = 0; i < childs.size(); i++)
	{
		Control* cld = childs[i].second;
		Vec2vbi point;
		int mx, my;
		SDL_GetMouseState(&mx, &my);
		point.x.val = mx;
		point.y.val = my;
		if (BSUtility::HitTest(point, cld->pos, cld->size).val)
		{
			cld->onKeyUp(key);
		}
	}
}
BlueprintScriptLibrary::Class* HorizentalBox::GetClass()
{
	return nullptr;
}
